package com.fiume.travel.service.impl;

import com.fiume.travel.constant.RedisConstant;
import com.fiume.travel.mapper.CategoryMapper;
import com.fiume.travel.pojo.Category;
import com.fiume.travel.pojo.CategoryExample;
import com.fiume.travel.service.CategoryService;
import com.fiume.travel.utils.BeanConv;
import com.fiume.travel.utils.EmptyUtil;
import com.fiume.travel.vo.CategoryVo;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * Description:分类模块的实现类
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-02-21 20:31
 * @see com.fiume.travel.service.impl
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    RedissonClient redissonClient;
    /**
     * @Description 查询所分类
     */
    @Override
    public List<CategoryVo> findAllCategory() {

        // 先查询redis
        String key = RedisConstant.CATEGORYSERVICE_FINDALLCATEGORY;
        RBucket<List<CategoryVo>> bucket = redissonClient.getBucket(key);
        List<CategoryVo> categoryVoList = bucket.get();
        if(!EmptyUtil.isNullOrEmpty(categoryVoList)){
            return categoryVoList;
        }

        // 在redis中查不到数据则在mysql数据库中查询
        CategoryExample example = new CategoryExample();
        //由于不需要条件 故可以直接查询
        List<Category> categoryList = categoryMapper.selectByExample(example);
        //将数据存入redis中
        if(!EmptyUtil.isNullOrEmpty(categoryList)){
            bucket.set(BeanConv.toBeanList(categoryList,CategoryVo.class),1800, TimeUnit.SECONDS);
        }
        return BeanConv.toBeanList(categoryList,CategoryVo.class);


    }
}
